import 'dart:convert';
import 'dart:io';
import 'data.dart';

main() async{
    var requestServer = await HttpServer.bind('192.168.31.183', 8080);
    print('http服务启动起来');
    await for(HttpRequest request in requestServer){
        handleMessage(request);

    }
}
void handleMessage(HttpRequest request){
    try{

          if(request.method == 'GET'){
            handleGET(request);
          }else if(request.method == 'POST'){
            handleGET(request);
          }

    }catch(e){
        print("捕获了一个异常:$e");
    }



}

void handleGET(HttpRequest request){
  //获取请求参数
  var action = request.uri.queryParameters['action'];

  if(action == 'getProducts'){

var page = request.uri.queryParameters['page'];


    print("第$page 页数据："+json.encode(products));
    request.response
    ..statusCode=HttpStatus.ok
    ..write(json.encode(products))
    ..close();
  }
  if(action == 'getNews'){
      print('获取新闻数据');
        request.response
        ..statusCode=HttpStatus.ok
        ..write(json.encode(news))
        ..close();
        }  
        
        else if(action == 'contactCompany'){

          var msg = request.uri.queryParameters["msg"];

      print('客户留言为$msg');
        request.response
        ..statusCode=HttpStatus.ok
        ..write("提交成功：$msg")
        ..close();
        }

  // request.response..write('get request success...')..close();
}

void handlePOST(HttpRequest request){

}